#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 
import struct
from PySide6.QtGui import QPalette, QColor
from PySide6.QtWidgets import QTableWidgetItem
from PySide6 import QtWidgets
 
class Devlog:
    def __init__(self, ui):
        self.ui = ui
        self.com = ui.com
        self.table = ui.tableWidget
        
        self.setupui()
        self.UISend_button1 = self.ui.pushButton_11 # 读取按钮
        self.UISend_button1.clicked.connect(self.ui_pushButton1_cb)
 
    def setupui(self):
        self.table.setRowCount(1)
        if self.table.rowCount() > 0:        
            self.table.scrollToBottom() # 滚动到最后
        self.table.setColumnWidth(0, 100)
        self.table.setColumnWidth(1, 100)
        self.table.setColumnWidth(2, 100)
        self.table.setColumnWidth(3, 400)
        self.table.setColumnWidth(4, 100)
        self.table.setColumnWidth(5, 100)
        self.table.setColumnWidth(6, 100)
        self.table.setColumnWidth(7, 100)
        self.table.setStyleSheet("QTableView::item { text-align: center; }")

    def ui_pushButton1_cb(self):
        self.ui_cb_send_readmsg()
        
    def ui_cb_send_readmsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x37)
            #self.pack_matain_msg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
            # print(' '.join(f'{c:02x}' for c in array))
        else:
            pass  
        self.table.clearContents()
        self.table.setRowCount(0)
         
    def ui_tableadd(self, a):
        nb = a[7]
        total = a[8]
        node = a[9]
        node_type = a[10]
        node_val = a[11]
        time_h = struct.unpack('<H', a[13:15])[0]
        time_m = a[15]
        time_s = a[16]
        data0 = int(struct.unpack('<h', a[17:19])[0] / 10)
        data1 = int(struct.unpack('<h', a[19:21])[0] / 10)
        data2 = struct.unpack('<h', a[21:23])[0]
        data3 = struct.unpack('<h', a[23:25])[0]
  
        dict_fault = {
            "0" : "压缩机无输出",
            "1" : "氧浓度低", 
            "2" : "氧压力低",
            "3" : "过滤器寿命到期",
            "4" : "压缩机过温/过流", 
            "5" : "压力传感器异常",
            "6" : "超声波传感器故障",
            "7" : "分子筛阀故障", 
            "8" : "出气阀故障", 
            "9" : "压差传感器故障", 
            "16" : "设备上电",
            "17" : "通道1工作模式变化", 
            "18" : "通道2工作模式变化",
        }
        dict_mode = {
            "0" : ":无效",
            "1" : ":待机", 
            "2" : ":肺式供氧",
            "3" : ":无效",
            "4" : ":连续供氧", 
            "5" : ":无效",
            "6" : ":应急模式",
            "7" : ":维护模式", 
            "8" : ":无效"
        }

        fault_res_str = [":恢复", ""] 
        node_str = dict_fault[str(node)]
        if node_type == 1:
            node_str+= fault_res_str[node_val]
        elif  node_type == 2: 
            node_str+= dict_mode[str(node_val)]
        itmes = [QTableWidgetItem(f'{nb}'), 
            QTableWidgetItem(f'{time_h}:{time_m:02d}:{time_s:02d}'), 
            QTableWidgetItem(f'{node}'), 
            QTableWidgetItem(node_str), 
            QTableWidgetItem(f'{data0}'), 
            QTableWidgetItem(f'{data1}'), 
            QTableWidgetItem(f'{data2}'), 
            QTableWidgetItem(f'{data3}'),  
        ]
        
        #self.table.setRowCount(self.table.rowCount()+1) 
        raw = self.table.rowCount()
        self.table.insertRow(raw)
        raw = self.table.rowCount() - 1
        for i in range(len(itmes)): 
            itmes[i].setTextAlignment(Qt.AlignCenter)
            self.table.setItem(raw, i, itmes[i])  

        if (raw+1) >= total:
            time.sleep(0.05)
            self.table.scrollToBottom() # 滚动到最后 
   
            
    def decode(self, a):
        if self.table.rowCount() > 100:
            return
        self.ui_tableadd(a)    
        #if self.table.rowCount() > 0:        
        #    self.table.scrollToBottom() # 滚动到最后
        

     


           

